<?php

// This is a PLUGIN TEMPLATE.

// Copy this file to a new name like abc_myplugin.php.  Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt

// Plugin name is optional.  If unset, it will be extracted from the current
// file name. Uncomment and edit this line to override:
$plugin['name'] = 'wyn_recent_comments';

$plugin['version'] = '0.1';
$plugin['author'] = 'wayne';
$plugin['author_uri'] = 'http://gopherwood.info/';
$plugin['description'] = 'Get recent comments list, with contents displayed';

// Plugin types:
// 0 = regular plugin; loaded on the public web side only
// 1 = admin plugin; loaded on both the public and admin side
// 2 = library; loaded only when include_plugin() or require_plugin() is called
$plugin['type'] = 0; 


@include_once('../zem_tpl.php');

if (0) {
?>
# --- BEGIN PLUGIN HELP ---

<h2>Description</h2>

<p>Fine, textpattern has already provided a recent comments tag. But someone love it and someone hate it. And it indeed is not so powerful enough. So, this thing was born.</p>

<h2>Usage</h2>

<ol>
	<li>class: css class, default is wyn_recent_comments</li>
	<li>wraptag: tag of wrap, default is ul</li>
	<li>break: tag of every item, default is li</li>
	<li>label: title label, default is nothing</li>
	<li>labeltag: tag of title, default is nothing</li>
	<li>limit: number of comments to show, default is 10</li>
	<li>wordcount: the length of each comment. The comment whose length is longer than this value, it will be cut off, with <span class="caps">UTF</span>-8 support. default is 10</li>
	<li>messagebreak: if a comment has been cut off, this value will be showed after it. default is &#8216;&#8230;&#8217;</li>
	<li>format: the things to show. you can customize it by using tags (<code>%content%</code> for comment content, <code>%commenter%</code> for commenter name, <code>%time%</code> for comment time and <code>%title%</code> for post title). default is <code>%content%</code></li>
	<li>atitle: the title attribute of generated a links. tags can also be used here. default is <code>%commenter% on %time%</code></li>
	<li>section: limit the comments into one section</li>
	<li>timeformat: the format of time. it uses standard <span class="caps">PHP</span> time format, you can refer <span class="caps">PHP</span> doc to know more. default is &#8216;%d %b %Y &#183; %X&#8217;, a little difficult :D</li>
</ol>


# --- END PLUGIN HELP ---
<?php
}

# --- BEGIN PLUGIN CODE ---

function wyn_recent_comments ($atts, $thing) {
	extract (lAtts (array (		
		'class'    => __FUNCTION__,
		'wraptag' => 'ul',
		'break'  => 'li',
		'label' => '',
		'labeltag' => '',
		'limit'    => 10,
		'wordcount' => 10,
		'messagebreak' => '...',
		'format' => '%content%',
		'atitle' => '%commenter% on %time%',
		'section' => '',
		'timeformat' => '%d %b %Y &#183; %X',
		), $atts));
		
	/* create sql things */
	$cols = 'ID, name, unix_timestamp(txp_discuss.posted) as time, message, Title';
	$tables = safe_pfx_j ('txp_discuss, textpattern');
	$where = 'parentid=ID and visible=1' . ('' == trim ($section) ? '' : " and Section='$section'") . ' order by time desc limit ' . $limit;
	
	$sql = 'SELECT ' . $cols . ' FROM ' . $tables . ' WHERE ' . $where . ';';

	$rs = startRows ($sql);
	if ($rs) {
		$out = array ();
		while ($o = nextRow ($rs)) {
			extract ($o);
			
			$time = safe_strftime($timeformat, $time);
			
			/* 0 if for no limit */
			if ($wordcount != 0) {
				$message = trim (doStripTags ($message));
				$message = utf8_substr ($message, 0, $wordcount, $messagebreak);
			}

			$atitleattr = preg_replace ('/%title%/', $Title, $atitle);
			$atitleattr = preg_replace ('/%commenter%/', $name, $atitleattr);
			$atitleattr = preg_replace ('/%time%/', $time, $atitleattr);
			$atitleattr = preg_replace ('/%content%/', $message, $atitleattr); /* who will use content here ??? */
		
			$cont = preg_replace ('/%title%/', $Title, $format);
			$cont = preg_replace ('/%commenter%/', $name, $cont);
			$cont = preg_replace ('/%time%/', $time, $cont);
			$cont = preg_replace ('/%content%/', $message, $cont);
			
			$link = '<a href="' . permlinkurl_id ($ID) . '" title="' . $atitleattr . '">' . $cont . '</a>';

			$out[] = $link;
		}
		
		return doLabel ($label, $labeltag) . doWrap ($out, $wraptag, $break, $class);
	}
}

function utf8_substr($string, $start, $length, $wordbreak) {
	/* just return original thing if length is 0 or less than 0 */
	if ($length <= 0) {
		return $string;
	}

	preg_match_all ('/[\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF][\x80-\xBF][\x80-\xBF]/', $string, $rs);

	$out = '';
        $size = count ($rs[0]);

	$end = $start + $length;
	if ($end > $size - 1) {
		$end = $size;
	}

	for ($i = $start; $i < $end; $i++) {
      	        $out .= $rs[0][$i];
	}

	if ($end < $size - 1) {
		$out .= $wordbreak;
	}

	return $out;
}

# --- END PLUGIN CODE ---

?>
